/*
 * This file is part of the Android Dimensions Editor Project: http://code.google.com/p/android-dimension-editor/
 *  
 * Copyright (c) 2013 Rick Falck
 *  
 * This code is licensed under the terms of the Apache License Version 2.0.
 * You may use this code according to the license.
 *
 * The terms of the license can be found in the root directory of this project's repository as well as at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under these Licenses is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See each License for the specific language governing permissions and
 * limitations under that License.
 */
package MessageBox;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 * <b><i>public class MessageBoxController implements Initializable</i></b>
 * <p>
 * This class is created by the FXML loader.
 * @author Rick Falck
 */
public class MessageBoxController implements Initializable {

    @FXML
    private Button btnNo;

    @FXML
    private Button btnYes;

    @FXML
    private ImageView imgMessage;

    @FXML
    private TextArea txtMessage;

    @FXML
    private ComboBox<String> mComboBox;

    @FXML
    private TextField mUserTextField;

    // Fields set by user before showing the box
    private Stage myStage;
    private String message = "";
    private MBOX_TYPE mbType = null;
    private ArrayList<String> mComboBoxStrings = null;

    // Fields retrieved
    private boolean bOK;
    private String mComboBoxSelectedText;

    private String sMessageTextStyle = null;

    public MessageBoxController(Stage stage) {
        this.myStage = stage;
    }

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle arg1) {
        this.myStage.setResizable(false);
        this.txtMessage.setText(message);

        mComboBox.getSelectionModel().selectedItemProperty().addListener(effect1ItemSelected);

        if (mComboBoxStrings != null) {
            this.mComboBox.getItems().addAll(mComboBoxStrings);
            mUserTextField.setVisible(false);
        } else {
            mComboBox.setVisible(false);
        }

        if (this.sMessageTextStyle == null) {
            this.txtMessage.setStyle("-fx-font-family: Ariel; -fx-font-size: 18px; -fx-font-weight: bold; -fx-text-fill: black;");
        } else {
            this.txtMessage.setStyle(sMessageTextStyle);
        }

        try {
            switch (this.mbType) {
                case CONFIRMATION:
                    mUserTextField.setVisible(false);
                    this.btnNo.setVisible(true);
                    this.btnYes.setVisible(true);
                    this.btnYes.setText("Yes");
                    break;
                case GET_USER_TEXT:
                case GET_USER_SELECTION:
                    this.btnNo.setVisible(true);
                    this.btnYes.setVisible(true);
                    this.btnYes.setText("OK");
                    break;
                case ERROR:
                    mUserTextField.setVisible(false);
                    this.btnNo.setVisible(false);
                    this.btnYes.setVisible(true);
                    this.btnYes.setText("OK");
                    break;
                default:
                case INFORMATION:
                    mUserTextField.setVisible(false);
                    this.btnNo.setVisible(false);
                    this.btnYes.setVisible(true);
                    this.btnYes.setText("OK");
                    break;
            }

            this.btnYes.requestFocus();

        } catch (Exception e) {
            this.txtMessage.setText(e.toString());
        }
    }

    public void OnBtnYes(ActionEvent event) {
        this.bOK = true;
        this.myStage.close();
    }

    public void OnBtnNo(ActionEvent event) {
        this.bOK = false;
        this.myStage.close();
    }

    // User setup methods
    public void setMessage(String message) {
        this.message = message;
    }

    public void setMbType(MBOX_TYPE mbType) {
        this.mbType = mbType;
    }

    public void setsMessageTextStyle(String sMessageTextStyle) {
        this.sMessageTextStyle = sMessageTextStyle;
    }

    public void setComboBoxStrings(ArrayList<String> comboBoxStrings) {
        mComboBoxStrings = comboBoxStrings;
    }

	// User response methods
    public boolean userClickedOK() {
        return this.bOK;
    }

    public String getComboBoxSelectedText() {
        return mComboBoxSelectedText;
    }

    public String getTextFieldText() {
        return mUserTextField.getText();
    }

    public final ChangeListener<String> effect1ItemSelected = new ChangeListener<String>() {

        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue,
                String newValue) {
            mComboBoxSelectedText = mComboBox.getValue();
        }
    };

    public enum MBOX_TYPE {

        INFORMATION, ERROR, CONFIRMATION, GET_USER_TEXT, GET_USER_SELECTION, HELP
    }
}
